package leetcode.editor.cn.q1xx

import leetcode.editor.cn.TreeNode
import java.util.*

class Q107 {

    fun levelOrderBottom(root: TreeNode?): List<List<Int>> {
        val result = LinkedList<List<Int>>()
        if (root == null) {
            return result
        }
        val queue = LinkedList<TreeNode>() as Queue<TreeNode>
        queue.offer(root)
        while (queue.isNotEmpty()) {
            val level = arrayListOf<Int>()
            for (i in queue.indices) {
                val node = queue.poll()
                level.add(node.`val`)
                if (node.left != null) {
                    queue.offer(node.left)
                }
                if (node.right !=null)
                    queue.offer(node.right)
            }
            result.add(0, level)
        }
        return result
    }
}